package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.xinyan.XinyanBlackRecord;
import com.ucredit.babel.common.model.third.xinyan.XinyanData;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 *  新颜负面拉黑接口
 *
 * @author renzhanzheng
 */

public class XinYanBlackListParse extends ParseBase  {
    private Text val = new Text();
    private Text key = new Text();

    /**
     *
     * 新颜负面拉黑接口
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        XinyanBlackRecord obj = (XinyanBlackRecord)model;
        XinyanData xinyanData = obj.getXinyanData();
        JSONObject info = xinyanData.getData() == null ? new JSONObject() : JSON.parseObject(xinyanData.getData());
        JSONObject resultDetail = info.getJSONObject("result_detail") == null ? new JSONObject() : info.getJSONObject("result_detail");

        StringBuilder sb = getBaseInfo(obj, successful);
        //查询结果码
        sb.append(info.getString("code") == null ? "" : info.getString("code")).append(CRT001);
        //查询结果描述
        sb.append(info.getString("desc") == null ? "" : info.getString("desc")).append(CRT001);
        //身份证号
        sb.append(info.getString("id_no") == null ? "" : info.getString("id_no")).append(CRT001);
        //姓名
        sb.append(info.getString("id_name") == null ? "" : info.getString("id_name")).append(CRT001);
        //版本号
        sb.append(info.getString("versions") == null ? "" : info.getString("versions")).append(CRT001);
        //收费标示
        sb.append(info.getString("fee") == null ? "" : info.getString("fee")).append(CRT001);
        //交易流水号
        sb.append(info.getString("trade_no") == null ? "" : info.getString("trade_no")).append(CRT001);
        //商户请求订单号
        sb.append(info.getString("trans_id") == null ? "" : info.getString("trans_id")).append(CRT001);
        //当前逾期机构数
        sb.append(resultDetail.getString("currently_overdue") == null ? "" : resultDetail.getString("currently_overdue")).append(CRT001);
        //睡眠机构数
        sb.append(resultDetail.getString("acc_sleep") == null ? "" : resultDetail.getString("acc_sleep")).append(CRT001);
        //当前履约机构数
        sb.append(resultDetail.getString("currently_performance") == null ? "" : resultDetail.getString("currently_performance")).append(CRT001);
        //异常还款机构数
        sb.append(resultDetail.getString("acc_exc") == null ? "" : resultDetail.getString("acc_exc")).append(CRT001);
        //最大逾期金额
        sb.append(resultDetail.getString("max_overdue_amt") == null ? "" : resultDetail.getString("max_overdue_amt")).append(CRT001);
        //最长逾期天数
        sb.append(resultDetail.getString("max_overdue_days") == null ? "" : resultDetail.getString("max_overdue_days")).append(CRT001);
        //最近逾期时间
        sb.append(resultDetail.getString("latest_overdue_time") == null ? "" : resultDetail.getString("latest_overdue_time")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_xinyan_blacklist_inc";
    }

    @Override
    public String getType() {
        return RecordType.XINYAN_BLACK_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "新颜负面拉黑接口";
    }
}
